/**
 * 在线练习相关脚本函数
 */
// 读出学生给出的练习答案（以“题目ID__回答@@”的形式组成字符串上传给action）
var xhr = false;
var options = ["A","B","C","D"];
function CreateXMLHttp() {
	try {
		xhr = new XMLHttpRequest();
	} catch (e) {
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				xhr = false;
			}
		}
	}
	return xhr;
}
function readExer() {
	var obj = document.forms;
	var str = "";
	for (i = 0; i < obj.length; i++) {
		var j;
		for (j = 0; j < obj[i].length; j++) {
			if (obj[i][j].checked == true) {
				str = str + obj[i][j].name + "__" + obj[i][j].value + "@@";
				break;
			}
		}
		if (j == obj[i].length) {
			str = str + obj[i][0].name + "__" + "" + "@@";
		}
	}
	xhr = CreateXMLHttp();
	xhr.open("POST",
			"./exercise!checkExercises.action?answers="
					+ str, true);
	xhr.onreadystatechange = checkExer;
	xhr.send();

}
//收到结果信息形式为：（例子）r__2@@w__1@@（r为正确，w为错误，数字为正确答案）
function checkExer() {
	if ((xhr.readyState == 4) && (xhr.status == 200)) {
		var respose = xhr.responseText;
		var res = new Array();
		res = respose.split("@@");
		for (i = 0;i < res.length;i++){
			var r = new Array();
			r =	res[i].split("__");
			var str = "result"+(i+1);
			if ("r"==r[0]){
				document.getElementById(str).innerHTML = "正确&nbsp正确答案为:"+options[r[1]-1];
			}else{
				document.getElementById(str).innerHTML = "错误&nbsp正确答案为:"+options[r[1]-1];	
			}
		}
	}
}